﻿Imports AForge.Imaging.Filters
Imports AForge
Imports System.Drawing.Imaging
Imports UCBase
Imports System.Drawing

Public Class ContrastBrightnessColor
    Implements I_ImageExchange
    Implements I_LoadSave
    Private WithEvents Inp As I_ImageExchange
    Private Backpic As New BackgroundCreator
    Private Mov As New Mover
    Private SourceBitmap As System.Drawing.Bitmap
    Private DestBitmap As System.Drawing.Bitmap

    Public Event ImageReady(ByVal Image As System.Drawing.Bitmap) Implements I_ImageExchange.ImageOut1
    Private Sub MoveToFilter()
        Dim Br As Double = KnobControl1.Value
        Dim Con As Double = KnobControl2.Value
        Dim col As Double = KnobControl3.Value / 100

        If SourceBitmap.PixelFormat <> PixelFormat.Format24bppRgb Then
            Dim temp As Bitmap = AForge.Imaging.Image.Clone(SourceBitmap, PixelFormat.Format24bppRgb)
            SourceBitmap.Dispose()
            SourceBitmap = temp
        End If

        Dim brfilter As BrightnessCorrection = New BrightnessCorrection(Br)
        Dim Tmp As Bitmap = brfilter.Apply(SourceBitmap)
        Dim confilter As ContrastCorrection = New ContrastCorrection(Con)
        Dim TmpB As Bitmap = confilter.Apply(Tmp)
        Dim colfilter As SaturationCorrection = New SaturationCorrection(col)
        DestBitmap = colfilter.Apply(TmpB)

        RaiseEvent ImageReady(DestBitmap)
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.BackgroundImage = Backpic.CreatePic(405, 160, Color.Orange)
        Mov.Connect(Me)
        KnobControl1.ContainerColor = Backpic.FaceColor
        KnobControl1.Refresh()
        KnobControl2.ContainerColor = Backpic.FaceColor
        KnobControl2.Refresh()
        KnobControl3.ContainerColor = Backpic.FaceColor
        KnobControl3.Refresh()

    End Sub
    Private Sub Socket1_Connect() Handles Socket1.Connect
        Dim S As Socket = Socket1.Remote
        Inp = S.Parent
    End Sub

    Private Sub Inp_ImageReady(ByVal Image As System.Drawing.Bitmap) Handles Inp.ImageOut1
        SourceBitmap = Image
        MoveToFilter()
    End Sub

    Private Sub ContrastFilter_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Width = 405
        Me.Height = 160

    End Sub
    Private Sub Socket1_Disconnected() Handles Socket1.Disconnected
        Inp = Nothing
    End Sub

    Private Sub Button3D5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D5.Click
        KnobControl1.Value = 0
    End Sub

    Public Function GetSettings() As String Implements UCBase.I_LoadSave.GetSettings
        Dim Out As String = KnobControl1.Value.ToString & ":" & KnobControl2.Value.ToString & ":" & KnobControl3.Value.ToString
        Return Out
    End Function

    Public Sub SetSettings(ByVal Settings As String) Implements UCBase.I_LoadSave.SetSettings
        Dim vals() As String = Settings.Split(":")
        KnobControl1.Value = Val(vals(0))
        KnobControl2.Value = Val(vals(1))
        KnobControl3.Value = Val(vals(3))
    End Sub

    Private Sub Button3D1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D1.Click
        KnobControl2.Value = 0
    End Sub

    Private Sub Button3D2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3D2.Click
        KnobControl3.Value = 0

    End Sub
End Class
